package com.fishery.processing.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fishery.entity.GlobalException;
import com.fishery.entity.StatusCode;
import com.fishery.processing.dao.CraftDao;
import com.fishery.processing.dao.FactoryDao;
import com.fishery.processing.dao.InputDao;
import com.fishery.processing.dao.ProductDao;
import com.fishery.processing.entity.Craft;
import com.fishery.processing.entity.Factory;
import com.fishery.processing.entity.vo.FactoryMessageVo;
import com.fishery.processing.entity.vo.FactoryOfBaseVo;
import com.fishery.processing.mapper.FactoryMapper;
import com.fishery.processing.service.FactoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-01-14
 */
@Service
public class FactoryServiceImpl extends ServiceImpl<FactoryMapper, Factory> implements FactoryService {

    @Autowired
    private FactoryDao factoryDao;

    @Autowired
    private CraftDao craftDao;

    @Autowired
    private ProductDao productDao;

    @Autowired
    private InputDao inputDao;

    public int createFactory(Factory factory){
        List<Factory> factories = factoryDao.findFactoryByBaseAndName(factory);

        //如果加工厂名称已存在
        if (!factories.isEmpty()){
            throw new GlobalException(StatusCode.DUPLICATE,"该加工厂已存在，无法修改");
        }

        //id置空 等待mybatis-plus填充
        factory.setId(null);
        return factoryDao.addFactory(factory);
    }

    @Transactional
    public int deleteFactory(String factoryId){

        //删除加工厂的所有工艺
        craftDao.removeCraftOfFactory(factoryId);

        //删除加工厂的所有产品
        productDao.removeProductOfFactory(factoryId);

        //删除加工厂的所有投入品
        inputDao.removeInputOfFactory(factoryId);
        return factoryDao.removeFactory(factoryId);
    }


    public int updateFactory(Factory factory){
        List<Factory> factories = factoryDao.findFactoryByBaseAndName(factory);

        //如果该基地已有该加工厂名称且id与要修改工厂的id不一致，抛出异常
        if (!factories.isEmpty() && !factories.get(0).getId().equals(factory.getId())){
            throw new GlobalException(StatusCode.DUPLICATE,"该加工厂名称已存在，无法修改");
        }

        return factoryDao.updateFactory(factory);
    }

    public Factory searchFactoryById(String factoryId){
        return factoryDao.findFactoryById(factoryId);
    }


    public IPage<Factory> searchFactoriesOfBase(String baseId, Integer page, Integer size) {
        return factoryDao.findFactoriesOfBase(baseId,page,size);
    }


    public List<Factory> searchFactoriesOfBase(String baseId) {
        return factoryDao.findFactoriesOfBase(baseId);
    }


    public List<Factory> searchFactoryByType(String type) {
        return factoryDao.findFactoryOfType(type);
    }


    public IPage<Factory> searchFactoryByType(String type, Integer page, Integer size) {
        return factoryDao.findFactoryOfType(type,page,size);
    }

    public FactoryOfBaseVo searchFactoryMessageOfBase(String baseId) {
        return factoryDao.findFactoryMessageOfBase(baseId);
    }

    public List<FactoryMessageVo> searchFactoryMessageOfAll() {
        return factoryDao.findFactoryMessageOfAll();
    }
}
